#include <unistd.h>
#include "stdio.h"
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"


#include <hi_types_base.h>
#include <hi_i2c.h>
#include <hi_early_debug.h>
#include <hi_stdlib.h>
#include "led.h"

typedef struct
{
    LED_ID_TYPE ledid;
    bool status;
} LEDInfo;

static volatile LEDInfo gLEDInfo[3] = {0};

void LEDonoff(LED_ID_TYPE ledid,bool enable)
{
    
    if(ledid == LED_ID_R){
        gLEDInfo[0].ledid = LED_ID_R;
        gLEDInfo[0].status = enable;
        GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, enable);
    }else if(ledid == LED_ID_G){
        gLEDInfo[1].ledid = LED_ID_Y;
        gLEDInfo[1].status = enable;
        GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, enable);
    }else if(ledid == LED_ID_Y){
        gLEDInfo[2].ledid = LED_ID_Y;
        gLEDInfo[2].status = enable;
        GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, enable);
    }
        
}

int LEDToggle(LED_ID_TYPE ledid)
{
    int ret = -1;
    if(ledid == LED_ID_R){
        if(gLEDInfo[0].status == 0){
            gLEDInfo[0].status = 1;
            GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 1);
            ret = 1;
        } else {
            gLEDInfo[0].status = 0;
            GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 0);
            ret = 0;
        }
        
    }else if(ledid == LED_ID_G){
        if(gLEDInfo[1].status == 0){
            gLEDInfo[1].status = 1;
            GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 1);
            ret = 1;
        } else {
            gLEDInfo[1].status = 0;
            GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 0);
            ret = 0;
        }
    }else if(ledid == LED_ID_Y){
        if(gLEDInfo[2].status == 0){
            gLEDInfo[2].status = 1;
            GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 1);
            ret = 1;
        } else {
            gLEDInfo[2].status = 0;
            GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 0);
            ret = 0;
        }
    }
    return ret;   
}
int LEDStatus(LED_ID_TYPE ledid)
{
    int ret = -1;
    if(ledid == LED_ID_R){
        ret = gLEDInfo[0].status;
    }else if(ledid == LED_ID_G){
        ret = gLEDInfo[1].status;
    }else if(ledid == LED_ID_Y){
        ret = gLEDInfo[2].status;
    }
    return ret;
}

void LED_Init(void)
{
    GpioInit();
    //复用引脚为 GPIO 10 Red LED
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_IO_FUNC_GPIO_10_GPIO);
    //设置为输出
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT);
    //复用引脚为 GPIO11 Green LED
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
    //设置为输出
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT);
    //复用引脚为 GPIO12 Yellow LED
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
    //设置为输出
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT);
}
